<?php

require_once(dirname(__FILE__)."/userdao.php");
require_once(dirname(__FILE__)."/locationdao.php");
require_once(dirname(__FILE__)."/statusdao.php");
# user basic functions
class User
{
    const STATUS_HOME   = 1;
    const STATUS_WORK   = 2;
    const STATUS_PUBLIC = 4;

    const RET_SUCCESS          = 0;
    const ERROR_LOGIN          = -1;
    const ERROR_USER_EXIST     = -2;
    const ERROR_USER_NOT_EXIST = -3;
    const ERROR_GESTURE        = -4;
    const ERROR_MACADDRESS_NULL = -5;
    const ERROR_USERNAME_INVALID= -6;

    public function __construct()
    {
        $this->_username = "";
        $this->_passwd   = "";
        $this->_email    = "";
        $this->_getsture = "";
        $this->_status   = self::STATUS_PUBLIC;
        $this->_trialtimes = -1;
    }

    # get user location status
    public function getTrialTimes()
    {
        $statusDao = new UserStatusDao();
        $ret = $statusDao->getStatus($this->_username);
        if (!$ret)
        {
            $this->_trialtimes = 0;
        }
        else 
        {
            $this->_trialtimes = $ret->trial_times;
        }
        return $this->_trialtimes;
    }

    # increase the trail time of gesture
    public function increamentTrialTimes()
    {
        $statusDao = new UserStatusDao();
        # update trial times of gesture
        $this->getTrialTimes();
        $trialTimes = $this->_trialtimes + 1;

        $statusDao->updateTrialTimes($this->_username, $trialTimes);

    }

    # increase the trail time of gesture
    public function clearTrialTimes()
    {
        $statusDao = new UserStatusDao();
        $trialTimes = 0;
        $statusDao->updateTrialTimes($this->_username, $trialTimes);
    }

    # get user location status
    public function getStatus()
    {
        $statusDao = new UserStatusDao();
        $ret = $statusDao->getStatus($this->_username);
        if (!$ret)
        {
            $this->_status = self::STATUS_PUBLIC;
        }
        else 
        {
        	//$this->_status = $ret['location'];
            $this->_status = $ret->location;
        }
        return $this->_status;
    }

    # set user location status
    private function _setStatus($status)
    {
        $this->_status |= $status;
        return $this->_status;
    }

    # getuser info
    public function getUser($username)
    {
        $userDao = new UserDao();
        $user = $userDao->getUser($username);
        # user not exist
        if (!$user)
        {
            return self::ERROR_LOGIN;
        }

//        $this->_username = $user['username'];
//        $this->_passwd   = $user['passwd'];
//        $this->_getsture = $user['gesture'];
        $this->_username = $user->username;
        $this->_passwd   = $user->passwd;
        $this->_getsture = $user->gesture;

        # get user current status 
        $statusDao = new UserStatusDao();
        $status = $statusDao->getStatus($this->_username);
        if (!$status)
        {
            $location = self::STATUS_PUBLIC;
        }
        else
        {
            //$location = $status['location'] | self::STATUS_PUBLIC;
            $location = $status->location | self::STATUS_PUBLIC;
            $this->_setStatus($location);
        }
        return self::RET_SUCCESS;

    }

    # login 
    public function login($username, $passwd)
    {
        $userDao = new UserDao();
        $user = $userDao->getUser($username);
        # user not exist 
        if (!$user)
        {
            return self::ERROR_LOGIN;
        }
        # check passwd is correct
        //if ($user['passwd'] != $passwd)
        if ($user->passwd != $passwd)
        {
            return self::ERROR_LOGIN;
        }

//        $this->_username = $user['username'];
//        $this->_passwd   = $user['passwd'];
//        $this->_getsture = $user['gesture'];
        
        $this->_username = $user->username;
        $this->_passwd   = $user->passwd;
        $this->_getsture = $user->gesture;
        # get user current status 
        $statusDao = new UserStatusDao();
        $status = $statusDao->getStatus($this->_username);
        if (!$status)
        {
            $location = self::STATUS_PUBLIC;
        }
        else
        {
            $location = $status->location | self::STATUS_PUBLIC;
            $this->_setStatus($location);
        }
        return self::RET_SUCCESS;
    }

    # register
    public function register($username, $passwd, $email)
    {
        if (!$username)
        {
            return self::ERROR_USERNAME_INVALID;
        }
        $userDao = new UserDao();
        $ret = $userDao->addUser($username, $passwd, $email);
        # username exists, or email address is not valid
        # return -1;
//        if (!$ret)
//        {
//            return self::ERROR_USER_EXIST;
//        }

        return  self::RET_SUCCESS;
    }

    public function getMacAddress($location)
    {
        $localtionDao= new LocationDao();
        return $localtionDao->getMacAddress($this->_username, $location);
    }

    public function isBound($location)
    {
        $ret = $this->getMacAddress($location);
        if ($ret)
            return true;
        else
            return false;
    }

    # set user location
    public function setLocation($location)
    {
        # get macAddress from userstatus table
        $statusDao = new UserStatusDao();
        $ret = $statusDao->getStatus($this->_username);
        //if (!$ret || empty($ret['mac_address']))
        if (!$ret || empty($ret->mac_address))
        {
            return self::ERROR_MACADDRESS_NULL;
        }
        //$macAddress = $ret['mac_address'];
        $macAddress = $ret->mac_address;
        $localtionDao = new LocationDao();
        $status = $localtionDao->setLocation($this->_username, $macAddress, $location);
        return self::RET_SUCCESS;
    }

    # comunicate with client background application to get mac address
    public function updateStatus($username, $macAddress)
    {
        $user = new UserDao();
        $ret = $user->getUser($username);
        if (!$ret)
        {
            return self::ERROR_USER_NOT_EXIST;
        }
        $localtionDao = new LocationDao();
        $arrLocation = $localtionDao->getLocation($username, $macAddress);
        if (!$arrLocation)
        {
            $location = self::STATUS_PUBLIC;
        }
        else 
        {
        	//$location = $arrLocation['location'];
            $location = $arrLocation->location;
        }
        
        $statusDao = new UserStatusDao();
        $statusDao->setStatus($username, $location, $macAddress);
        return self::RET_SUCCESS;
    }

    public function getGesgure()
    {
        return $this->_getsture;
    }


    # set unlock gesture 
    # strGesture is a string of number range from 1~9
    public function setGesture($strGesture)
    {
        $userDao = new UserDao();
        $ret = $userDao->setGesture($this->_username, $strGesture);
//        if (!$ret)
//        {
//            return self::ERROR_USER_NOT_EXIST;
//        }

        return self::RET_SUCCESS;
    }

    # unlock
    public function unlockFromGesture($strGesture)
    {
        if ($strGesture != $this->_getsture)
        {
            return self::ERROR_GESTURE;
        }

        # update status 
        $statusDao = new UserStatusDao();
        $statusDao->setStatus($this->_username, self::STATUS_HOME|self::STATUS_WORK|self::STATUS_PUBLIC);
        return self::RET_SUCCESS;
    }
};
